#ifndef __TwBillboardChain_H__
#define __TwBillboardChain_H__

#include "TwMovableObject.h"
#include "TwRenderable.h"
#include "TwArray.h"
#include <vector>

class TwBillboardChain : public TwMovableObject, public TwRenderable
{
public:
	struct VERTEX
	{
		D3DXVECTOR3			pos;
		DWORD				color;
		D3DXVECTOR2			tex;

		static const DWORD	FVF =  D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;
	};

	enum TextureCoordMode
	{
		TextCoord_CustomX,
		TextCoord_CustomY
	};

	class Twement
	{
	public:
		Twement();
		Twement(D3DXVECTOR3 position,
			float width,
			float texCoord,
			ColorValue colour);

		D3DXVECTOR3 position;
		float width;
		float texCoord;
		ColorValue colour;
	};
	typedef std::vector<Twement> TwementList;

public:
	TwBillboardChain();
	virtual ~TwBillboardChain();

	virtual void setMaxChainTwements(size_t maxTwements);

	virtual void addChainTwement(const Twement& billboardChainTwement);
	virtual void removeChainTwement(size_t elementIndex);
	virtual size_t getNumChainTwements() const;
	virtual void updateChainTwement(size_t elementIndex, const Twement& billboardChainTwement);
	virtual const Twement& getChainTwement(size_t elementIndex) const;
	virtual void clearChainTwements();

	virtual void setTexture(IDirect3DTexture9* texture);
	virtual void setTextureCoordMode(unsigned char mode);

	virtual void setUseChainDirection(bool setting);

	void needUpdate();

	virtual void update();
	virtual void render(TwCamera* camera, TimeValue t);

	virtual void _updateRenderQueue(TwRenderQueue* queue, TwCamera* cam);
	virtual float getSquaredViewDepth(TwCamera* cam) const;

	virtual const TwAxisAlignedBox& getBoundingBox();

protected:
	virtual void setupChainContainers();
	void _setupVertices(TwCamera* cam);
	void _setupBoundingBox();

protected:
	bool			mNeedUpdateBounding;
	bool			mNeedUpdateVertices;

	size_t			mMaxTwements;

	// The list holding the chain elements
	TwementList		mChainTwementList;

	IDirect3DTexture9* mTexture;
	unsigned char	mTexCoordMode;
	TwArray<VERTEX>	mVertices;

	bool			mUseChainDirection;

	TwAxisAlignedBox mAABB;
};

#endif //__TwBillboardChain_H__